<template>
  <div class="cockpit-footer" @click.stop>
    <canvas class="canvas" id="pag-single-footer" style="z-indx: 200"></canvas>
    <div class="footer">
      <!-- 左侧展开按钮 -->
      <el-tooltip
        effect="dark"
        content="展开左侧菜单"
        placement="top"
        popper-class="left-Close-Btn"
      >
        <div v-if="leftClose" class="leftClose" @click.stop="openLeft"></div>
      </el-tooltip>
      <!-- 左侧菜单 -->
      <el-tabs
        v-model="leftActive"
        class="left"
        :class="{ leftCardClose: leftClose, leftCardOpen: !leftClose }"
        @mouseenter="closeIntivertLeft"
        @mouseleave="setInterLeft"
      >
        <el-tooltip effect="dark" content="收起" placement="top">
          <img
            src="/img/cockpit/rightClose.png"
            alt=""
            class="closeArrow"
            @click.stop="closeLeft"
          />
        </el-tooltip>
        <el-tooltip effect="dark" content="历史记录" placement="top">
          <img
            class="leftImg"
            src="/img/cockpit/history.png"
            v-if="leftActive === 1"
            alt=""
            @click.stop="$emit('waringClick')"
          />
        </el-tooltip>

        <el-tab-pane label="报警事件 " :name="1">
          <WarningEvents
            class="tabAnimation"
            :showHead="false"
            :isSmall="isSmall"
            @waringClick="$emit('waringClick')"
          />
        </el-tab-pane>
        <el-tab-pane label="航空器架次 " :name="2">
          <PlaneSorties
            class="tabAnimation"
            :showHead="false"
            :isSmall="isSmall"
          />
        </el-tab-pane>
        <el-tab-pane label="航线" :name="3">
          <AirLine class="tabAnimation" :showHead="false" :isSmall="isSmall" />
        </el-tab-pane>
      </el-tabs>
      <!-- 底部功能按钮 -->
      <PageChange :bottom="'14%'" />
      <!-- 右侧展开按钮 -->
      <el-tooltip
        effect="dark"
        content="展开右侧菜单"
        placement="top"
        popper-class="right-Close-Btn"
      >
        <div v-if="rightClose" class="rightClose" @click.stop="openRight"></div>
      </el-tooltip>
      <!-- 右侧菜单 -->
      <el-tabs
        v-model="rightActive"
        class="right"
        :class="{
          showOnly: !tabPaneShow,
          rightCardClose: rightClose,
          rightCardOpen: !rightClose,
        }"
        @mouseenter="closeIntiverRight"
        @mouseleave="setInterRight"
      >
        <el-tooltip effect="dark" content="收起" placement="top">
          <img
            src="/img/cockpit/closeArrow.png"
            alt=""
            class="closeArrow"
            @click.stop="closeRight"
          />
        </el-tooltip>
        <el-tooltip effect="dark" content="历史记录" placement="top">
          <img
            class="rightImg"
            :class="{ isSmall, onlyOne: !tabPaneShow }"
            src="/img/cockpit/history.png"
            v-if="rightActive === 1"
            alt=""
            @click.stop="$emit('flyPlanClick')"
        /></el-tooltip>
        <el-tooltip
          :disabled="tabPaneShow"
          effect="dark"
          content="返回统计页"
          placement="top"
        >
          <!-- 返回飞行任务统计 -->
          <img
            class="singleBack"
            v-if="!tabPaneShow"
            src="/img/cockpit/back.png"
            alt=""
            @click.stop="backToCount"
          />
        </el-tooltip>

        <el-tab-pane label="飞行计划" :name="1">
          <FlyPlan
            class="tabAnimation"
            ref="flyPlanRef"
            :showHead="false"
            :isSmall="isSmall"
            @tabPhoneHidden="tabPhoneHidden"
            @flyPlanClick="$emit('flyPlanClick')"
            @closeIntiver="closeIntiverRight"
            @openIntiver="setInterRight"
          />
        </el-tab-pane>
        <el-tab-pane v-if="tabPaneShow" label="执行架次" :name="2">
          <ExecuteSorties
            class="tabAnimation"
            :showHead="false"
            :isSmall="isSmall"
          />
        </el-tab-pane>
        <el-tab-pane v-if="tabPaneShow" label="飞行类型排行 " :name="3">
          <Ranking class="tabAnimation" :showHead="false" :isSmall="isSmall" />
        </el-tab-pane>
      </el-tabs>
      <!-- 企业标注 -->
      <div class="copyRight">技术支持©杰能科世智能安全科技(杭州)有限公司</div>
    </div>
  </div>
</template>
<script>
export default {
  name: "Footer2",
};
</script>
<script setup>
import { ref, defineProps, onMounted, onUnmounted } from "vue";
import WarningEvents from "./WarningEvents.vue";
import PlaneSorties from "./PlaneSorties.vue";
import AirLine from "./AirLine.vue";
import FlyPlan from "./FlyPlan.vue";
import ExecuteSorties from "./ExecuteSorties.vue";
import Ranking from "./Ranking.vue";
import PageChange from "./PageChange.vue";
import { PAGInit } from "libpag";
import useCockpitStore from "@/store/modules/cockpit";

const cockpitStore = useCockpitStore();
const leftActive = ref(1);
const rightActive = ref(1);
const leftClose = ref(true);
const rightClose = ref(true);
const tabPaneShow = ref(true);
const isLeftClose = ref(false);
const isRightClose = ref(false);

let pagView = null;

const props = defineProps({
  isSmall: {
    type: Boolean,
    default: false,
  },
});

const emits = defineEmits(["waringClick", "flyPlanClick", "footerHide"]);

let timerL;
let timerR;

function initialization() {
  PAGInit().then((PAG) => {
    let url = import.meta.env.VITE_BASE_PUBLICPATH + "/img/cockpit/footer.pag";
    fetch(url)
      .then((response) => response.blob())
      .then(async (blob) => {
        const file = new window.File(
          [blob],
          url.replace(/(.*\/)*([^.]+)/i, "$2")
        );
        const pagFile = await PAG.PAGFile.load(file);
        const canvas = document.getElementById("pag-single-footer");
        // 解决清晰度问题
        canvas.width = pagFile.width() * window.devicePixelRatio;
        canvas.height = pagFile.height() * window.devicePixelRatio;
        // canvas.style.position = "absolute";
        // 自适应
        pagView = await PAG.PAGView.init(pagFile, canvas, {
          useScale: false,
        });
        // 高清渲染
        // pagView = await PAG.PAGView.init(pagFile, "#pag");
        canvas.style.zIndex = 1;
        pagView.setRepeatCount(0);
        await pagView.play();
      });
  });
}

/**
 * @展开左侧菜单
 * */
function openLeft() {
  leftClose.value = false;
  // 报警统计数据
  cockpitStore.getAlarmNum();
  // 飞行器架次统计
  cockpitStore.getAircraftData();
  // 航线统计数据
  cockpitStore.getAirouteAsync();
  setInterLeft();
}

/**
 * @关闭左侧菜单
 * */
function closeLeft() {
  leftClose.value = true;
  isLeftClose.value = false;
  closeIntivertLeft();
}

/**
 * @展开右侧菜单
 * */
function openRight() {
  rightClose.value = false;
  // 飞行计划统计
  cockpitStore.getFlyPlanData();
  // 执行架次统计
  cockpitStore.getExecuteData();
  setInterRight();
  emits("footerHide");
}

/**
 * @关闭右侧菜单
 * */
function closeRight() {
  rightClose.value = true;
  emits("footerHide");
  closeIntiverRight();
}

/**
 * @开启左侧定时器
 * */
function setInterLeft() {
  if (leftClose.value) return;
  if (timerL) return;
  timerL = setInterval(() => {
    if (leftActive.value > 2) {
      leftActive.value = 1;
      return;
    }
    leftActive.value += 1;
  }, 15000);
}

/**
 * @开启右侧定时器
 * */
function setInterRight() {
  if (rightClose.value || !tabPaneShow.value) return;
  if (timerR) return;
  timerR = setInterval(() => {
    if (rightActive.value > 2) {
      rightActive.value = 1;
      return;
    }
    rightActive.value += 1;
  }, 15000);
}

/**
 * @关闭左侧定时
 * */
function closeIntivertLeft() {
  clearInterval(timerL);
  timerL = null;
}

/**
 * @关闭右侧定时
 * */
function closeIntiverRight() {
  clearInterval(timerR);
  timerR = null;
}

/**
 * @飞行计划统计下点击切换
 * */
function tabPhoneHidden(val) {
  tabPaneShow.value = val;
}

/**
 * @返回统计页
 * */
const flyPlanRef = ref();
const backToCount = () => {
  flyPlanRef.value.reback();
  tabPaneShow.value = true;
  setInterRight();
};

watch(leftActive, (newV) => {
  if (newV === 1) {
    // 报警统计数据
    cockpitStore.getAlarmNum();
  }
  if (newV === 2) {
    // 飞行器架次统计
    cockpitStore.getAircraftData();
  }
  if (newV === 3) {
    // 航线统计数据
    cockpitStore.getAirouteAsync();
  }
});

onMounted(() => {
  initialization();
  setInterLeft();
  setInterRight();
});
onUnmounted(() => {
  clearInterval(timerL);
  clearInterval(timerR);
});
</script>
<style lang="scss" scoped>
.cockpit-footer {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  //margin-left: 2%;
  height: 32%;
  background: linear-gradient(180deg, rgba(0, 0, 0, 0), rgba(0, 0, 0, 1));
  z-index: 2;
  display: flex;
  justify-content: center;
  pointer-events: none;
  //padding: 0 20px;

  canvas {
    width: 100%;
    display: block;
    margin: 0 auto;
    position: absolute;
    //display: flex;
    bottom: 0;
  }

  .footer {
    position: absolute;
    bottom: 0;
    width: 96%;
    height: 100%;
    display: flex;
    justify-content: space-between;
    background-repeat: no-repeat;
    background-size: auto;
    background-position: center bottom;
    // overflow: hidden;
    z-index: 10;

    .closeArrow {
      position: absolute;
      left: 1%;
      top: -30%;
      width: 50px;
      height: 50px;
      z-index: 199;
      cursor: pointer;
    }

    .leftImg {
      position: absolute;
      top: 0;
      right: 50px;
      z-index: 100;
      cursor: pointer;
    }

    .rightImg {
      //border: 1px red solid;
      position: absolute;
      //top: 10.5%;
      top: -28%;
      left: 28%;
      z-index: 100;
      cursor: pointer;
    }

    .copyRight {
      position: absolute;
      right: 50px;
      bottom: 10px;
      color: #fff;
      font-size: 12px;
    }

    .isSmall {
      // left: 175px;
      // left: 100%;
    }

    .onlyOne {
      left: calc(100% - 110px);
    }

    .left,
    .right {
      width: 33%;
      pointer-events: all;
      // display: none;
      // opacity: 0;
    }

    .left {
      position: absolute;
      bottom: 14%;
      left: 1.5%;
    }

    .right {
      position: absolute;
      bottom: 14%;
      right: 1.5%;

      .singleBack {
        position: absolute;
        top: -28%;
        right: 0;
        z-index: 100;
        cursor: pointer;
      }
    }

    .leftCardOpen {
      animation: leftCardOpen 1s 1 alternate linear;
      animation-fill-mode: forwards;
    }

    .leftCardClose {
      animation: leftCardClose 1s 1 alternate linear;
      animation-fill-mode: forwards;
    }

    .rightCardOpen {
      animation: rightCardOpen 1s 1 alternate linear;
      animation-fill-mode: forwards;
    }

    .rightCardClose {
      animation: rightCardClose 1s 1 alternate linear;
      animation-fill-mode: forwards;
    }
  }

  .leftClose {
    position: absolute;
    left: 1%;
    bottom: 3%;
    width: 100px;
    height: 100px;
    cursor: pointer;
    pointer-events: all;
    z-index: 1002;
    background: url("/img/cockpit/leftClose.png");
  }
  .leftClose:hover {
    background: url("/img/cockpit/leftCloseActive.png");
  }
  //.leftCloseActive {
  //  background: url("/img/cockpit/leftCloseActive.png");
  //}
  .rightClose {
    position: absolute;
    right: 1%;
    bottom: 3%;
    width: 100px;
    height: 100px;
    cursor: pointer;
    pointer-events: all;
    z-index: 1002;
    background: url("/img/cockpit/rightClose.png");
  }
  .rightClose:hover {
    background: url("/img/cockpit/rightCloseActive.png");
  }
  //.rightCloseActive {
  //  background: url("/img/cockpit/rightClose.png");
  //}
}

@keyframes leftCardOpen {
  0% {
    display: block;
    opacity: 0;
    -webkit-transform: translate(-200px, 100px) scale(0);
  }
  100% {
    opacity: 1;
    transform: translate(0) scale(1);
  }
}

@keyframes leftCardClose {
  0% {
    opacity: 1;
    -webkit-transform: translate(0) scale(1);
  }
  100% {
    display: none;
    opacity: 0;
    transform: translate(-200px, 100px) scale(0);
  }
}

@keyframes rightCardOpen {
  0% {
    display: block;
    opacity: 0;
    -webkit-transform: translate(200px, 100px) scale(0);
  }
  100% {
    opacity: 1;
    transform: translate(0) scale(1);
  }
}

@keyframes rightCardClose {
  0% {
    opacity: 1;
    -webkit-transform: translate(0) scale(1);
  }
  100% {
    display: none;
    opacity: 0;
    transform: translate(200px, 100px) scale(0);
  }
}
</style>

<style lang="scss">
.left-Close-Btn {
  display: none;
}

.right-Close-Btn {
  display: none;
}

.cockpit-footer {
  .el-tabs .el-tabs__item {
    padding: 0;
    font-size: 24px;
    font-family: "YouSheBiaoTiHei";
    font-weight: 400;
    color: rgba(255, 255, 255, 0);
    // text-shadow: 0px 3px 2px #081b1e;
    background: linear-gradient(
      0deg,
      rgba(88, 130, 158, 0.96) 0%,
      rgba(255, 255, 255, 0.96) 100%
    );
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  }

  .el-tabs__header {
    position: relative;
    // left: -5%;
    margin: 0;
    background: url("/img/cockpit/titleBack.png");
    background-repeat: no-repeat;
    background-size: 100% auto;
    background-position: bottom;

    .el-tabs__nav-wrap {
      position: relative;
      top: -5px;
      left: 0;
      width: 100%;

      .el-tabs__nav {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0 15px;
        width: 100%;

        .el-tabs__item {
          margin: 0 50px;
        }
      }
    }

    .el-tabs__nav-wrap.is-scrollable {
      padding: 0;
    }

    .el-tabs__nav-wrap::after {
      display: none;
    }
  }

  .el-tabs__content {
    // position: relative;
    overflow: visible;
  }

  .el-tabs__active-bar {
    display: none;
  }

  .el-tabs__nav-prev,
  .el-tabs__nav-next {
    display: none;
  }

  .el-tabs__item.is-active {
    font-size: 24px;
    font-family: YouSheBiaoTiHei;
    font-weight: 400;
    color: rgba(255, 255, 255, 0);
    // text-shadow: 0px 1px 1px #117f97;

    background: linear-gradient(
      5deg,
      rgba(10, 152, 253, 0.96) 0%,
      rgba(255, 255, 255, 0.96) 100%
    );
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  }

  .tabAnimation {
    animation: anima 1s 1 alternate linear;
    animation-fill-mode: forwards;
  }

  .routeRight {
    position: absolute;
    top: -18%;
    width: 16px;
    left: 93%;
    height: 16px;
    cursor: pointer;
    z-index: 9999;
    background: url("/img/cockpit/routeRight.png") no-repeat;
  }

  @keyframes anima {
    0% {
      opacity: 0;
      -webkit-transform: translateX(200px) scale(0);
    }
    100% {
      opacity: 1;
      transform: translateX(0) scale(1);
    }
  }
}

.cockpit-footer .showOnly {
  .el-tabs__header .el-tabs__nav-wrap .el-tabs__nav {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    width: 100%;
  }
}
</style>
